package org.vergil.messagelistener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.lang.Nullable;

public class RedisMessageListener implements MessageListener {

    private Logger log = LoggerFactory.getLogger(RedisMessageListener.class);

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    RedisSerializer serializer;

    @Override
    public void onMessage(Message message, @Nullable byte[] pattern) {
        serializer = redisTemplate.getValueSerializer();
        String messageStr = serializer.deserialize(message.getBody()).toString();
        String channel = message.getChannel().toString();
        log.info("message is {},channel is {}",messageStr,channel);
//        TestWebSocketHandle.sendMessageToUsers(messageStr);
    }


}
